package com.wei.file.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import lombok.extern.log4j.Log4j2;

import java.lang.reflect.Field;
import java.util.*;

import static org.apache.logging.log4j.message.MapMessage.MapFormat.JSON;

@Log4j2
public class CommonUtils {


    static class  JsonProcessingException2 extends JsonProcessingException{

        protected JsonProcessingException2(String msg) {
            super(msg);
        }
    }

    private static final ObjectMapper mapper = new ObjectMapper();

    public static String ObjectToJSON(Object object) {
        try {
            return mapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            System.out.println(e.toString());
            log.error("ObjectToJSON：json解析失败，类名{}", object.getClass());
        }
        return "{}";
    }
    public static String tryObjectToJSON(Object object) throws JsonProcessingException {
        return mapper.writeValueAsString(object);
    }

    public static <T> T JSONToObject(String json, Class<T> t) throws JsonProcessingException {
        return mapper.readValue(json, t);
    }
    public static <T> List<T> JSONToList(String json, Class<T> t) throws JsonProcessingException {
        if(json==null){
            throw new JsonProcessingException2("json text is null");
        }
        CollectionType listType =
                mapper.getTypeFactory().constructCollectionType(ArrayList.class, t);
        return mapper.readValue(json, listType);
    }

    public static String getRandomKey(int len) {
        assert len > 0;
        assert len < 32;
        return UUID.randomUUID().toString().replace("-", "").substring(0, len);
    }


    public static String getFileSuffix(String filename) {
        if (filename == null) {
            return "";
        }
        int index = filename.lastIndexOf(".");
        if (index == -1) {
            return "";
        }
        return filename.substring(index + 1);
    }



    /**
     * 转换为驼色
     * 例如 java_bean ->  javaBean
     *
     * @return {@link String}
     */
    public static String convertToCamelCase(String underScore) {
        StringBuilder result = new StringBuilder();
        boolean nextUpperCase = false;
        for (int i = 0; i < underScore.length(); i++) {
            char currentChar = underScore.charAt(i);
            if (currentChar == '_') {
                nextUpperCase = true;
            } else {
                if (nextUpperCase) {
                    result.append(Character.toUpperCase(currentChar));
                    nextUpperCase = false;
                } else {
                    result.append(Character.toLowerCase(currentChar));
                }
            }
        }
        return result.toString();
    }


    private static final Map<Class<?>, Field[]> updateModelClassMap = new HashMap<>();


}
